package com.mengya.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.os.Build;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class AndroidInfoUtil {
	private static int screenWidth = 0;
	private static int screenHeight = 0;
	private static int screenHeight1 = 0;

	public static void setScreenHeight1(int screenHeight1) {
		AndroidInfoUtil.screenHeight1 = screenHeight1;
	}

	public static int getScreenHeight1() {
		// return screenHeight-screenHeight1;
		return screenHeight;
	}

	public static int getScreenWidth() {
		return AndroidInfoUtil.screenWidth;
	}

	public static void setScreenWidth(int screenWidth) {
		AndroidInfoUtil.screenWidth = screenWidth;
	}

	public static int getScreenHeight() {
		return screenHeight;
	}

	public static void setScreenHeight(int screenHeight) {
		AndroidInfoUtil.screenHeight = screenHeight;
	}

	public static boolean checkScreenSize() {
		if (AndroidInfoUtil.getScreenWidth() < 320
				|| AndroidInfoUtil.getScreenHeight() < 480) {
			System.out.println("版本太低不支持");
			return false;
		} else {
			System.out.println("版本支持");
			return true;
		}
	}

	public synchronized String run(String[] cmd, String workdirectory)
			throws IOException {
		String result = "";
		try {
			ProcessBuilder builder = new ProcessBuilder(cmd);
			InputStream in = null;
			// 设置一个路径
			if (workdirectory != null) {
				builder.directory(new File(workdirectory));
				builder.redirectErrorStream(true);
				Process process = builder.start();
				in = process.getInputStream();
				byte[] re = new byte[1024];

				while (in.read(re) != -1)
					result = result + new String(re);
			}
			if (in != null) {
				in.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	// 获取屏幕大小context
	public static int getScreenWidth(Context context) {
		WindowManager mWindowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		return mWindowManager.getDefaultDisplay().getWidth();
	}

	public static int getScreenHeight(Context context) {
		WindowManager mWindowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		return mWindowManager.getDefaultDisplay().getHeight();
	}

	// 获得像素密度
	public static float getxdpi(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		return dm.xdpi;
	}

	// 获得像素密度
	public static float getydpi(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		return dm.ydpi;
	}

	// 获得像素密度比例
	public static float getDensity(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		return dm.density;
	}

	public static String getUniqueSn(Context con) {
		return Secure.getString(con.getContentResolver(), Secure.ANDROID_ID);
	}

	// IMEI号，IESI号，手机型号等
	public static String getPhoneInfo(Context con) {
		String imei = "";
		try {
			TelephonyManager mTm = (TelephonyManager) con
					.getSystemService(Context.TELEPHONY_SERVICE);
			// imei = mTm.getDeviceId();
			imei = mTm.getSubscriberId();
			// String mtype = android.os.Build.MODEL; // 手机型号
			// String numer = mTm.getLine1Number(); // 手机号码，有的可得，有的不可得
			// return "imei" + imei + "imsi" + imsi + " mtype" + mtype + "numer"
			// + numer;
		} catch (Exception e) {
			imei = "padinfonoimei";
		}
		return imei;
	}

	public static int getAndroidSDKVersion() {
		int version = 0;
		try {
			version = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
		}
		return version;
	}

	public static String getAndroidOsVersion() {
		String version = "2.1";
		try {
			version = Build.VERSION.RELEASE;
		} catch (NumberFormatException e) {
		}
		return version;
	}

	public static String getManufacturerName() {
		return Build.MANUFACTURER;
	}

	public static String getModelName() {
		return Build.MODEL;
	}

	public static String getProductName() {
		return Build.PRODUCT;
	}

	// 运营商信息中包含IMEI、手机号码等
	public static String fetch_tel_status(Context cx) {
		String result = null;
		TelephonyManager tm = (TelephonyManager) cx
				.getSystemService(Context.TELEPHONY_SERVICE);
		String str = " ";
		str += "DeviceId(IMEI) = " + tm.getDeviceId() + " ";
		str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + " ";
		// TODO: Do something ...
		int mcc = cx.getResources().getConfiguration().mcc;
		int mnc = cx.getResources().getConfiguration().mnc;
		str += "IMSI MCC (Mobile Country Code): " + String.valueOf(mcc) + " ";
		str += "IMSI MNC (Mobile Network Code): " + String.valueOf(mnc) + " ";
		result = str;
		return result;
	}

	// 获得版本信息
	public static String fetch_version_info() {
		String result = null;
		AndroidInfoUtil cmdexe = new AndroidInfoUtil();
		try {
			String[] args = { "/system/bin/cat", "/proc/version" };
			result = cmdexe.run(args, "system/bin/");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return result;
	}

	// 获得显示屏信息
	public static String getDisplayMetrics(Context cx) {
		String str = "";
		DisplayMetrics dm = new DisplayMetrics();
		dm = cx.getApplicationContext().getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		float density = dm.density;
		float xdpi = dm.xdpi;
		float ydpi = dm.ydpi;
		str += "The absolute width: " + String.valueOf(screenWidth) + "pixels ";
		str += "The absolute heightin: " + String.valueOf(screenHeight)
				+ "pixels ";
		str += "The logical density of the display. : "
				+ String.valueOf(density) + " ";
		str += "X dimension : " + String.valueOf(xdpi) + "pixels per inch ";
		str += "Y dimension : " + String.valueOf(ydpi) + "pixels per inch ";
		return str;

	}

}